package com.woniu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.project.client.ProductEntityCient;
import com.woniu.project.core.domain.Product;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dao.ProductMapper;
import com.woniu.project.dto.product.ProductSelectDto;
import com.woniu.project.params.product.ProductAddParam;
import com.woniu.project.params.product.ProductSelectParam;
import com.woniu.project.params.product.ProductUpdateParam;
import com.woniu.project.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private ProductEntityCient productEntityCient;

    //根据不同条件进行不同的分页查询
    @Override
    public IPage<ProductSelectDto> select(ProductSelectParam productSelectParam) {
        Page<Product> page = new Page<>(productSelectParam.getPage(), productSelectParam.getSize());
        QueryWrapper<Product> wrapper = new QueryWrapper<Product>();
        wrapper.eq(productSelectParam.getId() != null && !productSelectParam.getId().equals(""),
                "product_id", productSelectParam.getId());

        wrapper.eq(productSelectParam.getProductSmallTypeId() != null && !productSelectParam.getProductSmallTypeId().equals(""),
                "product_small_type_id", productSelectParam.getProductSmallTypeId());

        wrapper.eq(productSelectParam.getProductBrandId() != null && !productSelectParam.getProductBrandId().equals(""),
                "product_brand_id", productSelectParam.getProductBrandId());

        wrapper.like(productSelectParam.getName() != null && !productSelectParam.getName().equals(""),
                "product_name", productSelectParam.getName());

        wrapper.orderByAsc("product_sort");
        IPage ipage = page(page, wrapper);
        //entity换为dto
        List<Product> records = ipage.getRecords();
        List<ProductSelectDto> productSelectDtos = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            ProductSelectDto productSelectDto = new ProductSelectDto();
            BeanUtils.copyProperties(records.get(i), productSelectDto);
            productSelectDtos.add(productSelectDto);
        }
        ipage.setRecords(productSelectDtos);
        return ipage;
    }

    //新增商品类型
    @Override
    public int addProduct(ProductAddParam productAddParam) {
        Product product = new Product();
        BeanUtils.copyProperties(productAddParam, product);

        //查询所有类型序号，如有序号等于当前序号则返回1
        QueryWrapper<Product> wrapper = new QueryWrapper<Product>();
        wrapper.eq("product_sort", product.getProductSort());
        List<Product> list = list(wrapper);
        if (list.size() != 0) {
            return 1;
        }
        //类型增加
        save(product);
        return 0;
    }

    //修改商品类型
    @Override
    public int updateProduct(ProductUpdateParam productUpdateParam) {
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateParam, product);

        //查询所有类型序号，如有序号等于当前序号则返回1
        QueryWrapper<Product> wrapper = new QueryWrapper<Product>();
        wrapper.eq("product_sort", product.getProductSort());
        wrapper.ne("product_id", productUpdateParam.getProductId());
        List<Product> list = list(wrapper);
        if (list.size() > 0) {
            return 1;
        }
        //类型增加
        updateById(product);
        return 0;
    }

    //根据id删除类型
    @Override
    public void deleteById(Integer id) {
        ResultDto resultDto = productEntityCient.selectByProductId(id);
        if (!resultDto.getData().toString().equals("[]")){
            throw new ProjectProductException(500, "该类型下还有商品，不能删除");
        }
        //删除类型
        removeById(id);
    }
}
